// 2014-02-01 Tyrghen
//---------------------------------------------
// Usage:
//  [] call edt_fnc_;
//---------------------------------------------
// Purpose:
// 
// 
// Parameters:
// - 
//
// Returns:
// 
//---------------------------------------------
// Objectif:
// 
//
// Paramètres:
// - 
//
// Renvoie:
// 
//---------------------------------------------

//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "editor\fn_ammobox.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_box","_side","_ammoboxes","_publishFromServer"];
DEFAULT_PARAM(_box, 0, objNull);
DEFAULT_PARAM(_side, 1, GVAR(side_own));
DEFAULT_PARAM(_publishFromServer, 2, false);

if (isNil QGVAR(LOADED) || {!GVAR(LOADED)}) then {
	_this spawn {
		waituntil { !(isNil QGVAR(LOADED)) && { GVAR(LOADED) } };
		_this call EFUNC(ammobox);
	};
} else {
	if !(isNil "ioc_fnc_exec") then {
		if (IS_OBJECT(_box) && {!(isNull _box)}) then {
			if !(OGETVAR(_box,GVAR(REGISTERED_AMMOBOX),false)) then {
				if (GVAR(SERVER)) then {
						_ammoboxes = [TMF_SERVER_INSTANCE_ID, QGVAR(AMMOBOXES),[]] call FFUNC(getMissionJIPVar);
						_ammoboxes set [count _ammoboxes, [_box,_side]];
						[TMF_SERVER_INSTANCE_ID, QGVAR(AMMOBOXES),_ammoboxes] call FFUNC(setMissionJIPVar);
						OSETVARP(_box,QGVAR(REGISTERED_AMMOBOX),true);
						if (_publishFromServer) then {
							TRACE_1("Publishing to clients",_box);
							["MSO","FillAmmoBox",[_box,_side],2] call ioc_fnc_rpc;
						};
				};
				if (GVAR(CLIENT)) then {
					["MSO","FillAmmoBox",[_box,_side]] call ioc_fnc_exec;
				};
			} else {
				ERROR_1("Box already registered", _box);
			};
		};
	} else {
		ERROR_1("IOC Not initialized",_this);
	};
};

TRACE("End");
TMFPROFILERSTOP;